
package com.beself.service.impl;

import com.beself.dao.TMemberMapper;
import com.beself.dao.TMineMapper;
import com.beself.dao.TProblemMapper;
import com.beself.entity.TMine;
import com.beself.entity.TProblem;
import com.beself.service.MineService;
import com.beself.until.PareDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Member;

import java.util.Date;
import java.util.List;

@Service
public class MineServiceImpl implements MineService {

    @Autowired
    private TMineMapper tMineMapper;

    @Resource
    private TProblemMapper tProblemMapper;

    @Resource
    private TMemberMapper tMemberMapper;

    @Override
    public TMine findMine(Integer uId) {
        TMine tMine = tMineMapper.selectByPrimaryKey(uId);
        return tMine;
    }

    @Override
    @Transactional
    public boolean updateMine(TMine tMine) {
        int i = tMineMapper.updateMine(tMine);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public Boolean insertProblem(TProblem tProblem) {

        int i = tProblemMapper.insertSelective(tProblem);

        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public TMine showSuperUser(TMine tMine) {
        Integer uid = tMine.getuId();
        TMine mine = tMineMapper.selectByPrimaryKey(uid);
        Integer count = mine.getmReplenish();
        Integer ismember = mine.getmIsmember();
        //不是会员
        if (ismember == 1) {
            //判断是否是新用户   打8折
            if (mine.getmReplenish() == 0) {
                mine.setDiscount(0.8);
            } else {
                mine.setDiscount(1.0);
            }
            //查询充值次数  对5求余   等于0 有折扣 不等于0 没有折扣 打9折
            if (count > 0) {
                if (count % 5 == 0) {
                    mine.setDiscount(0.9);
                } else {
                    mine.setDiscount(1.0);
                }
            }
        } else {     //是会员
            //查询会员用户到期的时间
            //查询充值次数  对5求余   等于0 有折扣 不等于0 没有折扣
            if (count > 0) {
                if (count % 5 == 0) {
                    mine.setDiscount(0.9);
                } else {
                    mine.setDiscount(1.0);
                }
            }
        }
        if (mine.getmMedate()== new Date()){
             int  i =tMineMapper.closeMember(uid);
        }
        return mine;
    }

    @Override
    public List<Member> showMember() {

        List<Member> list = tMemberMapper.getList();
        return list;
    }

    @Override
    @Transactional
    public Boolean updateIsmember(TMine mine) {
        //用户编号 套餐编号
        TMine tMine = tMineMapper.selectByPrimaryKey(mine.getuId());
        Integer ismember = tMine.getmIsmember();
        Date date = tMine.getmMedate();
        Integer meId = mine.getMeId();
        int i = -1;
        if (ismember == 1) {   //不是会员
            setMeId(meId,new Date(),tMine);
        } else {    //是会员用户
            setMeId(meId,date,tMine);
        }
        i = tMineMapper.updateMemberOfMine(tMine);
        if (i > 0) {
            return true;
        }
        return false;
    }
    /**
     * 开通套餐
     * @param meId
     */
    public Date setMeId(Integer meId,Date date,TMine tMine){
        Date newDate = null;
        switch (meId) {
            case 1:     //开通一个月
                newDate = PareDate.getNowDate(30 * 1, date);
                tMine.setMeId(meId);
                break;
            case 2:     //开通两个月
                newDate = PareDate.getNowDate(30 * 3, date);
                tMine.setMeId(meId);
                break;
            case 3:   //开通12个月
                newDate = PareDate.getNowDate(30 * 12, date);
                tMine.setMeId(meId);
                break;
        }
        tMine.setmMedate(newDate);
        return newDate;
    }
}